from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver import ChromeOptions
from time import sleep
import openpyxl
import csv

# 创建 WebDriver 对象，指明使用chrome浏览器驱动
option=ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])
option.add_experimental_option("detach",True)
option.add_argument("--disable-blink-features=AutomationControlled")
wd = webdriver.Chrome(service=Service(r'D:\soft\selenium\chromedriver.exe'),options=option)
script='''object.defineProperty(navigator,'webdriver',{undefinedget:()=>undefined}'''
wd.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",{"source":script})

# 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
wd.get('https://*********.cn/')

#窗口最大化
wd.maximize_window()

# #创建文件对象
# book = openpyxl.Workbook()
# sh=book.active
row=1

#列表主页界面
mainWindow = wd.current_window_handle

wd.implicitly_wait(10)


#进入登录，输入账号密码
wd.find_element(By.XPATH,'/html/body/section[1]/div/div/div/button').click()

wd.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div[3]/span[2]').click()
wd.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div[5]/div[1]/input').send_keys('**********')
wd.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div[5]/div[2]/input').send_keys('*********')
wd.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div[5]/button').click()

#跳过广告
root=wd.find_element(By.ID,'__qiankun_microapp_wrapper_for_big_member_sub_app__')
shadow_root=root.shadow_root
shadow_root.find_element(By.CSS_SELECTOR,'section > div.el-dialog__wrapper.guide-intro-dialog > div > div.el-dialog__body > div > button').click()
shadow_root.find_element(By.CSS_SELECTOR,'div div.el-dialog__body > div > div > img').click()

#进入搜索栏
wd.find_element(By.XPATH,'/html/body/section/section/aside/div/div[1]/div[1]/div[3]/span').click()
wd.switch_to.frame(wd.find_element(By.CSS_SELECTOR,'#work-bench-container > iframe'))
wd.find_element(By.XPATH,'//*[@id="searchinput"]').send_keys('工业互联网')
wd.find_element(By.XPATH,'//*[@id="zbSeatchT"]/input[2]').click()
wd.find_element(By.XPATH,'//*[@id="searchInner"]/div[2]/div[2]/div[2]/div[6]/div[2]/font[20]').click()
wd.find_element(By.XPATH,'//*[@id="searchInner"]/div[2]/div[2]/div[2]/div[2]/div[3]/font[48]').click()
wd.find_element(By.XPATH,'//*[@id="minprice"]').send_keys(500)
wd.find_element(By.XPATH,'//*[@id="minprice"]').click()
wd.find_element(By.XPATH,'//*[@id="maxprice"]').send_keys(50000000)
wd.find_element(By.XPATH,'//*[@id="searchInner"]/div[2]/div[2]/div[2]/div[3]/div[2]/div/button').click()
sleep(1)

list_all_project=[]
with open('1.csv','r',encoding='utf-8-sig') as f:
    reader=csv.reader(f)
    for row_list in reader:
        list_all_project.append(row_list)

try:
    while True:
        if row == 339:
            break

        #精简列表

        for i in range(0,50):
            if row < 53:
                row += 1
                continue
            else:
                pass
            sleep(1)
            elements = wd.find_elements(By.CSS_SELECTOR, '#allnews > div.lucene > ul > li div > div > div.bid-list-title > div > a')
            elements[i].click()

            windows=wd.window_handles
            wd.switch_to.window(windows[-1])
            sleep(1)

            #查文本
            wd.switch_to.frame(0)
            sleep(1)
            try:
                purchasing_unit = wd.find_element(By.XPATH,'//*[@id="tab2"]/div[2]/table/tbody/tr[3]/td[2]/span/span/div/div[1]').text
            except:
                purchasing_unit =''
            try:
                winning_bidder=wd.find_element(By.XPATH,'//*[@id="bidInfoCont"]/table/tbody[1]/tr/td[2]/span/span/div').text
            except:
                winning_bidder=''
            try:
                bid_winning_amount=wd.find_element(By.XPATH,'//*[@id="bidamount"]').text
            except:
                bid_winning_amount=''
            try:
                project_name=wd.find_element(By.XPATH,'//*[@id="com-title"]').text
            except:
                project_name=''
            try:
                project_time=wd.find_element(By.CSS_SELECTOR,'#statusbar > span.com-time').text
            except:
                project_time=''

            print(f'采购单位：{purchasing_unit},中标单位：{winning_bidder},中标金额：{bid_winning_amount},项目名称：{project_name}，时间：{project_time}')

            list_one_project=[purchasing_unit,winning_bidder,bid_winning_amount,project_name,project_time]
            list_all_project.append(list_one_project)

            #退出新标签页，回到主页面
            sleep(1)
            wd.close()
            row += 1
            wd.switch_to.window(mainWindow)
            sleep(1)

            #这里的每次跳转都是frame进行了跳转，因此需要switch_to.frame（）
            wd.switch_to.frame(wd.find_element(By.CSS_SELECTOR,'#work-bench-container > iframe'))
            sleep(1)

        #翻页
        wd.find_element(By.XPATH,'//*[@id="searchInner"]/div[2]/div[5]/div[7]/div[1]/button[2]').click()
        sleep(2)
except Exception as e:
    print(f'something wrong {e}')

with open("1.csv", "w", newline='',encoding='utf-8-sig') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(list_all_project)
print('Done--------------------')